package com.loubobooo.personal.blog.controller.backend;

import com.loubobooo.personal.blog.entity.Link;
import com.loubobooo.personal.blog.entity.vo.LinkVO;
import com.loubobooo.personal.blog.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller @RequestMapping("/admin/link") public class LinkManageController {

    @Autowired private LinkService linkService;

    //后台链接列表显示
    @RequestMapping(value = "") public ModelAndView linkList() {
        ModelAndView modelandview = new ModelAndView();

        List<LinkVO> linkVOList = linkService.listLink(null);
        modelandview.addObject("linkVOList", linkVOList);

        modelandview.setViewName("Admin/Link/index");
        return modelandview;

    }

    //后台添加链接页面显示
    @RequestMapping(value = "/insert") public ModelAndView insertLinkView() {
        ModelAndView modelAndView = new ModelAndView();

        List<LinkVO> linkVOList = linkService.listLink(null);
        modelAndView.addObject("linkVOList", linkVOList);

        modelAndView.setViewName("Admin/Link/insert");
        return modelAndView;
    }

    //后台添加链接页面提交
    @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) public String insertLinkSubmit(Link link) {
        link.setLinkCreateTime(new Date());
        link.setLinkUpdateTime(new Date());
        link.setLinkStatus(1);
        linkService.insertLink(link);
        return "redirect:/admin/link/insert";
    }

    //删除链接
    @RequestMapping(value = "/delete/{id}") public String deleteLink(@PathVariable("id") Integer id) {

        linkService.deleteLink(id);
        return "redirect:/admin/link";
    }

    //编辑链接页面显示
    @RequestMapping(value = "/edit/{id}") public ModelAndView editLinkView(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        LinkVO linkVO = linkService.getLinkById(id);
        modelAndView.addObject("linkVO", linkVO);

        List<LinkVO> linkVOList = linkService.listLink(null);
        modelAndView.addObject("linkVOList", linkVOList);

        modelAndView.setViewName("Admin/Link/edit");
        return modelAndView;
    }

    //编辑链接提交
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) public String editLinkSubmit(Link link) {
        link.setLinkUpdateTime(new Date());
        linkService.updateLink(link);
        return "redirect:/admin/link";
    }
}
